package com.ruoyi.eduadmin.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eduadmin.domain.ItChangeClassLog;
import com.ruoyi.eduadmin.domain.ItClasses;
import com.ruoyi.eduadmin.domain.ItFocus;
import com.ruoyi.eduadmin.dto.ChangeClassDto;
import com.ruoyi.eduadmin.dto.ChangeFocusDto;
import com.ruoyi.eduadmin.dto.StateDto;
import com.ruoyi.eduadmin.mapper.ItChangeClassLogMapper;
import com.ruoyi.eduadmin.mapper.ItClassesMapper;
import com.ruoyi.eduadmin.mapper.ItFocusMapper;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eduadmin.mapper.ItStudentesMapper;
import com.ruoyi.eduadmin.domain.ItStudentes;
import com.ruoyi.eduadmin.service.IItStudentesService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-27
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ItStudentesServiceImpl implements IItStudentesService 
{
    @Autowired
    private ItStudentesMapper itStudentesMapper;

    @Autowired
    private ItChangeClassLogMapper itChangeClassLogMapper;

    @Autowired
    private ItFocusMapper itFocusMapper;

    @Autowired
    private ItClassesMapper itClassesMapper;
    /**
     * 查询学生管理
     * 
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public ItStudentes selectItStudentesById(Long id)
    {
        return itStudentesMapper.selectItStudentesById(id);
    }

    /**
     * 查询学生管理列表
     * 
     * @param itStudentes 学生管理
     * @return 学生管理
     */
    @Override
    public List<ItStudentes> selectItStudentesList(ItStudentes itStudentes)
    {
        return itStudentesMapper.selectItStudentesList(itStudentes);
    }

    /**
     * 新增学生管理
     * 
     * @param itStudentes 学生管理
     * @return 结果
     */
    @Override
    public int insertItStudentes(ItStudentes itStudentes)
    {
        return itStudentesMapper.insertItStudentes(itStudentes);
    }

    /**
     * 修改学生管理
     * 
     * @param itStudentes 学生管理
     * @return 结果
     */
    @Override
    public int updateItStudentes(ItStudentes itStudentes)
    {
        return itStudentesMapper.updateItStudentes(itStudentes);
    }

    /**
     * 批量删除学生管理
     * 
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    public int deleteItStudentesByIds(Long[] ids)
    {
        return itStudentesMapper.deleteItStudentesByIds(ids);
    }

    /**
     * 删除学生管理信息
     * 
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteItStudentesById(Long id)
    {
        return itStudentesMapper.deleteItStudentesById(id);
    }

    /**
     * 批量修改状态
     * @param stateDto
     * @return
     */
    @Override
    public int changeStatus(StateDto stateDto) {
        return itStudentesMapper.changeStatus(stateDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeClass(ChangeClassDto changeClassDto) {
        /**
         * 组装学生信息
         */
        ItStudentes itStudentes = new ItStudentes();
        itStudentes.setId(changeClassDto.getId());
        itStudentes.setClassId(changeClassDto.getClassId());
        itStudentes.setStudentStatusId(changeClassDto.getStudentStatusId());
        itStudentesMapper.updateItStudentes(itStudentes);
        //修改原班级人数
        ItClasses oldClass = itClassesMapper.selectItClassesById(changeClassDto.getOldClassId());
        oldClass.setNumberPeople(oldClass.getNumberPeople()-1);
        itClassesMapper.updateItClasses(oldClass);
        //修改新班级人数
        ItClasses newClass = itClassesMapper.selectItClassesById(changeClassDto.getClassId());
        newClass.setNumberPeople(newClass.getNumberPeople()+1);
        itClassesMapper.updateItClasses(newClass);
        /**
         * 组装转班记录
         */
        ItChangeClassLog itChangeClassLog = new ItChangeClassLog();
        BeanUtils.copyProperties(changeClassDto,itChangeClassLog);
        itChangeClassLog.setToClassId(changeClassDto.getClassId());
        itChangeClassLog.setToClass(itClassesMapper.selectItClassesById(changeClassDto.getClassId()).getClassName());
        itChangeClassLog.setOldClass(itClassesMapper.selectItClassesById(changeClassDto.getOldClassId()).getClassName());
        return itChangeClassLogMapper.insertItChangeClassLog(itChangeClassLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeFocus(ChangeFocusDto changeFocusDto) {
        itStudentesMapper.changeFocus(changeFocusDto.getId(),changeFocusDto.getIsFocus());
        ItFocus itFocus = new ItFocus();
        BeanUtils.copyProperties(changeFocusDto,itFocus);
        itFocus.setTeacherName("测试"); // 后期老师模块添加后修改 TODO
        itFocus.setDate(new Date());
        itFocus.setStudentId(changeFocusDto.getId());
        itFocus.setStudentName(changeFocusDto.getName());
        if(changeFocusDto.getIsRepetition()){
            itStudentesMapper.changeRepetition(changeFocusDto.getId(),changeFocusDto.getIsRepetition());
        }
        return itFocusMapper.insertItFocus(itFocus);
    }

    @Override
    public int deductPoints(Long studentId, Integer deductPoints) {
        ItStudentes itStudentes = itStudentesMapper.selectItStudentesById(studentId);
        itStudentes.setScore(itStudentes.getScore() - deductPoints);
        return itStudentesMapper.updateItStudentes(itStudentes);
    }

    @Override
    public int changeFocus(Long studentId, Integer isFocus) {
        return itStudentesMapper.changeFocus(studentId,isFocus);
    }
}
